import regionApi from '@/api/basebiz/RegionApi'
import { buildTree, deepClone, getParentsById } from '@/utils/common'

export default {
  state: {
    regionTree: [],
    baseRegionList: [],
    areaTree: [],
    regionStationTree: [],
    stationTree: [],
    onlyRegionTree: [],
    stationData: {} // 记录新增，修改，删除站点时操作的站点对象
  },
  mutations: {
    // SET_BASE_REGION_LIST: (state, data) => (state.baseRegionList = data),
    SET_BASE_REGION_LIST: (state, data) => {
      data = data || state.baseRegionList
      data.forEach(i => {
        const parent = getParentsById(state.regionTree, i.regionId, 'key') || []
        i.parentIds = parent.map(
          j => j.regionId
        )
      })
      state.baseRegionList = data
    },
    SET_REGION_TREE: (state, data) => (state.regionTree = data),
    SET_AREA_TREE: (state, data) => (state.areaTree = data),
    SET_REGION_STATION_TREE: (state, data) => (state.regionStationTree = data),
    SET_STATION_TREE: (state, data) => (state.stationTree = data),
    SET_STATION_INFO: (state, data) => (state.stationData = data),
    SET_ONLY_REGION_TREE: (state, data) => (state.onlyRegionTree = data),
    clearRegion: state => {
      state.regionTree = []
      state.baseRegionList = []
      state.areaTree = []
      state.regionStationTree = []
      state.stationTree = []
      state.onlyRegionTree = []
      state.stationData = {}
    }
  },
  actions: {
    getRegions: ({ commit }, data) => {
      regionApi.list().then(res => {
        res = res || []
        res.forEach(i => delete i.children)
        localStorage.baseRegionList = JSON.stringify(res)
        // 保存原始数据
        commit('SET_BASE_REGION_LIST', deepClone(res))
        // 所有region,station,room
        // commit(
        //   'SET_REGION_TREE',
        //   buildTree(res, 'regionId', 'parentId', 'children')
        // )
        // region的树
        // const regionData = res.filter(i => i.regionType === 'REGION')
        // commit(
        //   'SET_AREA_TREE',
        //   buildTree(regionData, 'regionId', 'parentId', 'children')
        // )
        // region,station的树
        const regionStationData = res.filter(i => i.regionType !== 'ROOM')
        // commit(
        //   'SET_REGION_STATION_TREE',
        //   buildTree(regionStationData, 'regionId', 'parentId', 'children')
        // )

        // region下面有station的树
        // TODO 待优化算法
        const stationList = regionStationData.filter(
          i => i.regionType === 'STATION'
        )
        const allData = []
        stationList.forEach(i => {
          const findParent = id => {
            const parent = regionStationData.find(z => z.regionId === id)
            if (parent) {
              if (parent.parentId !== '0') findParent(parent.parentId)
              allData.push(parent)
            }
          }
          i.parentId !== '0' && findParent(i.parentId)
          allData.push(i)
        })
        const stationData = []
        allData.forEach(i => {
          if (!stationData.some(j => j.regionId === i.regionId)) {
            stationData.push(i)
          }
        })

        commit(
          'SET_STATION_TREE',
          buildTree(stationData, 'regionId', 'parentId', 'children')
        )
      }).catch(err => {
        console.log(err)
      })
      regionApi.tree(data || '').then(res => {
        res = res || []
        // 1. 所有region,station,room
        commit('SET_REGION_TREE', deepClone(res))
        commit('SET_BASE_REGION_LIST', null)

        // 2. 所有region
        const filterTree = (list, arr, level) => {
          list.forEach(ele => {
            ele.level = level
            if (ele.regionType !== 'STATION' && ele.regionType !== 'ROOM') {
              if (ele.children && ele.children.length) {
                const children = filterTree(ele.children, [], ele.level + 1)
                if (children.length) {
                  arr.push({ ...ele, children: children })
                } else {
                  delete ele.children
                  arr.push(ele)
                }
              } else {
                delete ele.children
                arr.push(ele)
              }
            }
          })
          return arr
        }
        commit('SET_AREA_TREE', filterTree(deepClone(res), []))
        // 3. 所有region,station
        const filterTree2 = (list, arr, level) => {
          list.forEach(ele => {
            ele.level = level
            if (ele.regionType !== 'ROOM') {
              if (ele.children && ele.children.length) {
                const children = filterTree2(ele.children, [], ele.level + 1)
                if (children.length) {
                  arr.push({ ...ele, children: children })
                } else {
                  delete ele.children
                  ele.regionType !== 'REGION' && arr.push(ele)
                }
              } else {
                delete ele.children
                ele.regionType !== 'REGION' && arr.push(ele)
              }
            }
          })
          return arr
        }
        commit('SET_REGION_STATION_TREE', filterTree2(deepClone(res), []))

        // 2. 所有region
        const filterTree3 = (list, arr, level) => {
          list.forEach(ele => {
            ele.level = level
            if (ele.regionType !== 'STATION' && ele.regionType !== 'ROOM') {
              if (ele.children && ele.children.length) {
                const children = filterTree3(ele.children, [], ele.level + 1)
                if (children.length) {
                  arr.push({ ...ele, children: children })
                } else {
                  delete ele.children
                  arr.push(ele)
                }
              } else {
                delete ele.children
                arr.push(ele)
              }
            }
          })
          return arr
        }
        commit('SET_ONLY_REGION_TREE', filterTree3(deepClone(res), [], 1))
      }).catch(err => {
        console.log(err)
      })
    }
  },
  getters: {
    regionTree: state => state.regionTree,
    areaTree: state => state.areaTree,
    baseRegionList: state => state.baseRegionList,
    regionStationTree: state => state.regionStationTree,
    stationTree: state => state.regionStationTree,
    stationData: state => state.stationData,
    onlyRegionTree: state => state.onlyRegionTree
  }
}
